<template>
    <div class="link-main">
        <el-card class="link-content">
            <h1>友情链接</h1>
            <h3 class="directory1">友链列表 <span class="num"> {{ linkList.length }} 条</span></h3>
            <div class="links">
                <a class="item" v-for="item in linkList" :key="item.id"
                   target="_blank" :href="item.url" :style="{ backgroundColor: `${randomColor()}` }">
                    <span class="name">{{ item.name }}</span>
                    <div class="item-content">
                        <div class="info">{{ item.info }}</div>
                        <img :src="item.avatar" alt="" />
                    </div>
                </a>
            </div>
            <div class="infoBox">
                <div style="display: flex;position: relative;">
                    <h3 class="directory2">申请格式</h3>
                    <div class="btn-box" @click="dialogFormVisible = true">
                        <i class="el-icon-circle-plus-outline"></i>
                        加入友链
                    </div>

                </div>

                <div class="site">
                    <span class="site-title"> 博客名称 + 博客地址 + 博客Logo + 博客简介 </span>
                    <span>博客名称：{{ webConfig.name }}</span>
                    <span>博客地址: <a :href="webConfig.webUrl" target="_blank">{{
                        webConfig.webUrl
                    }}</a> </span>
                    <span>博客Logo: <a :href="webConfig.logo" target="_blank">
                      {{ webConfig.logo}}</a>
                    </span>
                    <span>博客简介：{{ webConfig.summary }}</span>
                    <span style="color: red;padding-bottom: 10px;">
                        注:如果已经申请过友链 再次接入则会进行下架处理 需重新审核
                    </span>
                </div>
            </div>
        </el-card>

      <client-only>
        <el-dialog class="dialog" @close="closeDialog" v-model="dialogFormVisible">
          <el-form :model="form" :rules="rules" ref="ruleForm">
            <el-form-item label="网站名称" :label-width="formLabelWidth" prop="name">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="网站简介" :label-width="formLabelWidth" prop="info">
              <el-input v-model="form.info" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="网站地址" :label-width="formLabelWidth" prop="url">
              <el-input v-model="form.url" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="网站头像" :label-width="formLabelWidth" prop="avatar">
              <el-input v-model="form.avatar" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱地址" :label-width="formLabelWidth" prop="email">
              <el-input v-model="form.email" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitLink">确 定</el-button>
          </div>
        </el-dialog>
      </client-only>

    </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import {queryLinkList,addLink} from "~/api/links";
import {LinkVo} from "~/types/global";
import {useStore} from "@/store";
import {ElMessage} from "element-plus";

const commonStore = useStore();
const webConfig = computed(()=>commonStore.webSiteInfo.webConfig);
const linkList = ref<LinkVo[]>([]);
const dialogFormVisible = ref(false);
const formLabelWidth = ref('80px');
const generateFrom  = ()=>{
  return {
    name:'',
    info:'',
    url:'',
    avatar:'',
    email:''
  }
}
const form = ref(generateFrom());
const ruleForm = ref();
const rules = ref({
  name: [
    { required: true, message: '请输入网站名称', trigger: 'blur' },
  ],
  info: [
    { required: true, message: '请输入网站简介', trigger: 'blur' },
  ],
  url: [
    { required: true, message: '请输入网站地址', trigger: 'blur' },
  ],
  avatar: [
    { required: true, message: '请输入网站头像', trigger: 'blur' },
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
  ],
});
const randomColor= ():string=> {
  const letters = '0123456789ABCDEF';
  let color = '#';
  do {
    for (var i = 0; i < 6; i++) {
      color += letters[Math.floor(Math.random() * 16)];
    }
  } while (color === '#FFFFFF' || color === '#000000');
  return color;
}
const {data,refresh} = await queryLinkList();
linkList.value =data.value.data
const closeDialog= ()=>{
  form.value = generateFrom();
}
const submitLink= async ()=>{

  await ruleForm.value?.validate(async (valid: any, fields: any) => {
    if (valid) {
      await addLink(form.value);
      dialogFormVisible.value = false;
      ElMessage({
        type:'success',
        message:'操作成功'
      })

      refresh();
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>
<style lang="scss" scoped>
:deep(.el-form-item){
  margin-bottom: 18px !important;
}
:deep(.dialog-footer){
  display: flex;
  flex: 1;
  justify-content: flex-end;
  button{
    width: calc(100% / 4 - 30px);
  }
}
@media screen and (max-width: 1118px) {
    .link-main {
        display: flex;
        width:100%;
        :deep(.el-dialog) {
            width: 100%;
        }

        .link-content {
            background-color: var(--background-color);
            margin-top: 60px;
            width: 100%;

            h1 {
                width: 100%;
                font-size: 24px;
                color: var(--article-color);
                text-align: center;
                padding-top: 40px;
                margin-bottom: 15px;
                word-break: break-word;
                text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
                font-weight: 500;
            }

            .directory1,
            .directory2 {
                margin-left: 10px;
                margin-top: 20px;
                font-size: 1.3em;
                line-height: 24px;
                font-weight: 500;
                color: var(--article-color);

                &::before {
                    position: relative;
                    display: inline-block;
                    vertical-align: middle;
                    content: "";
                    top: -3px;
                    margin-right: 8px;
                    background-position: center;
                    width: 20px;
                    height: 20px;
                    background-size: auto 100%;
                    left: 0;
                    background-image: url();
                }

                .num {
                    position: relative;
                    top: -1px;
                    display: inline-block;
                    margin-left: 5px;
                    padding: 0 5px;
                    height: 18px;
                    line-height: 18px;
                    font-size: 12px;
                    color: #909399;
                    background: #f5f5f5;
                    border-radius: 3px;
                }
            }

            .links {
                margin-top: 20px;
                grid-template-columns: repeat(3, 1fr);
                display: grid;
                gap: 15px;
                margin-bottom: 50px;
                width: 100%;

                .item {
                    display: inline-block;
                    width: 100%;
                    height: auto;
                    border-top-left-radius: 10px;
                    border-bottom-right-radius: 30px;
                    transition: all 0.3s;
                    text-decoration: none;

                    &:hover {
                        transform: scale(1.1);
                    }

                    .item-content {
                        display: flex;
                        margin-top: 20px;
                    }

                    .name {
                        color: #fff;
                        border-bottom: 1px solid #fff;
                        font-weight: 700;
                        padding-bottom: 5px;
                        margin-left: 15px;
                        margin-top: 10px;
                        display: inline-block;

                    }

                    .info {
                        margin-left: 15px;
                        color: #fff;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        flex: 1;
                        margin-right: 10px;
                        word-break: break-all;
                    }

                    img {
                        width: 40px;
                        height: 40px;
                        border-radius: 50%;
                        object-fit: cover;
                        margin: 3px 0;
                        margin-right: 8px;

                    }
                }
            }

            .infoBox {

                .btn-box {
                    margin-left: auto;
                    margin-left: 10px;
                    color: var(--theme-color);
                    font-size: 16px;
                    margin-top: 20px;
                    position: absolute;
                    right: 0;
                    line-height: 24px;
                }
            }

            .site {
                background-color: #1d72f320;
                color: #1d72f3;
                border-left: 5px solid #1d72f3;
                margin-bottom: 50px;
                border-top-right-radius: 5px;
                border-bottom-right-radius: 5px;
                margin-top: 20px;
                min-width: 200px;

                a {
                    text-decoration: none;
                    color: var(--theme-color);
                    word-wrap: break-word;
                }

                .site-title {
                    font-weight: 700;
                    font-size: 15px
                }

                span {
                    padding: 5px 10px;
                    display: block;
                }
            }
        }
    }
}

@media screen and (min-width: 1119px) {
    .link-main {
        display: flex;
        justify-content: center;
        width:100%;
        :deep(.el-dialog){
            width: 30%;
            border-radius: 10px;
        }

        .link-content {
            padding: 20px;
            height: 100%;
            width: 70%;
            margin-top: 80px;
            background-color: var(--background-color);

            &:hover {
                box-shadow: 5px 4px 8px 6px rgba(7, 17, 27, .06);
                transition: all .3s;
            }

            h1 {
                font-size: 24px;
                color: var(--article-color);
                text-align: center;
                padding-top: 40px;
                margin-bottom: 15px;
                word-break: break-word;
                text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
                font-weight: 500;
            }

            .directory1,
            .directory2 {
                margin-left: 10px;
                margin-top: 20px;
                font-size: 1.3em;
                line-height: 24px;
                font-weight: 500;
                color: var(--article-color);

                &::before {
                    position: relative;
                    display: inline-block;
                    vertical-align: middle;
                    content: "";
                    top: -3px;
                    margin-right: 8px;
                    background-position: center;
                    width: 20px;
                    height: 20px;
                    background-size: auto 100%;
                    left: 0;
                    background-image: url();
                }

                .num {
                    position: relative;
                    top: -1px;
                    display: inline-block;
                    margin-left: 5px;
                    padding: 0 5px;
                    height: 18px;
                    line-height: 18px;
                    font-size: 12px;
                    color: #909399;
                    background: #f5f5f5;
                    border-radius: 3px;
                }
            }

            .links {
                margin-left: 40px;
                margin-top: 20px;
                grid-template-columns: repeat(4, 1fr);
                display: grid;
                gap: 15px;
                margin-bottom: 50px;

                .item {
                    display: inline-block;
                    width: 237px;
                    height: 118px;
                    border-top-left-radius: 10px;
                    border-bottom-right-radius: 30px;
                    transition: all 0.3s;
                    text-decoration: none;

                    &:hover {
                        transform: scale(1.1);
                    }

                    .item-content {
                        display: flex;
                        margin-top: 20px;
                    }

                    .name {
                        color: #fff;
                        border-bottom: 1px solid #fff;
                        font-weight: 700;
                        padding-bottom: 5px;
                        margin-left: 15px;
                        margin-top: 10px;
                        display: inline-block;

                    }

                    .info {
                        margin-left: 15px;
                        color: #fff;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        flex: 1;
                        margin-right: 10px;
                        word-break: break-all;
                    }

                    img {
                        width: 40px;
                        height: 40px;
                        border-radius: 50%;
                        object-fit: cover;
                        margin: 3px 0;
                        margin-right: 8px;

                    }
                }
            }

            .infoBox {

                .btn-box {
                    margin-left: auto;
                    margin-left: 10px;
                    color: var(--theme-color);
                    font-size: 16px;
                    margin-top: 20px;
                    position: absolute;
                    right: 0;
                    line-height: 24px;
                    cursor: pointer;
                }
            }

            .site {
                background-color: #1d72f320;
                color: #1d72f3;
                border-left: 5px solid #1d72f3;
                margin-left: 30px;
                margin-bottom: 50px;
                border-top-right-radius: 5px;
                border-bottom-right-radius: 5px;
                margin-top: 20px;

                a {
                    text-decoration: none;
                    color: var(--theme-color);
                }

                .site-title {
                    font-weight: 700;
                }

                span {
                    padding: 5px 10px;
                    display: block;
                }
            }
        }
    }
}
</style>
